<?php
/**
 * <b>Connection</b>.
 * Define la conexion del Sistema a la Base de Datos<br>
 */


class conexion {
   var  $db_connection = null;
    var $db_server = null;
    var $db_database = null;
    var $db_username = null;
    var $db_password = null;
    var $db_conectado= false;

    public function AbrirConexion($server, $database, $username, $password) {
        $this->db_server = $server;
        $this->db_database = $database;
        $this->db_username = $username;
        $this->db_password = $password;
        if (!$this->db_conectado) {
            try {
                $this->db_connection = mysql_pconnect ($this->db_server, $this->db_username, $this->db_password);
                mysql_select_db ($this->db_database);
                mysql_query ("SET NAMES 'utf8'");
                if (!$this->db_connection) {
                    $this->db_conectado = false;
                    throw new Exception('MySQL Connection Database Error: ' . mysql_error());
                }
                else {
                    $this->db_conectado = true;
                }
            }
            catch (Exception $e) {
                echo $e->GetMessage();
            }
        }
        else {
            return 'Error: No connection has been established to the database. Cannot open connection.';
        }
    }

    public function Close() {
        global $db_connection, $CONNECTED;
        if ($CONNECTED) {
            mysql_close($db_connection);
            $CONNECTED = false;
        }
        else {
            return 'Error: No connection has been established to the database. Cannot close connection.';
        }
    }
    public function __destruct() {
        unset($this->AbrirConexion);
        unset($this->Close);
        unset($this->Open);
        unset($this->db_connection);
        unset($this->db_server);
        unset($this->db_database);
        unset($this->db_username);
        unset($this->db_password);
        unset($this->CONNECTED);
    }
}
$_conexion = new conexion();
$_conexion->AbrirConexion(SERVER_NAME, DB_NAME, DB_USER_NAME, DB_PWD);

define('CONEXION',$_conexion->db_connection);
?>